<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java线程创建方式详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background-color: #282c34;
            border-radius: 8px;
            position: relative;
        }
        .code-header {
            background-color: #1e2127;
            padding: 8px 12px;
            border-top-left-radius: 8px;
            border-top-right-radius: 8px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .code-copy-btn {
            color: #abb2bf;
            cursor: pointer;
            transition: all 0.2s;
        }
        .code-copy-btn:hover {
            color: #fff;
        }
        pre {
            margin: 0;
            padding: 16px;
            overflow-x: auto;
        }
        .method-card {
            transition: transform 0.3s, box-shadow 0.3s;
            border-radius: 12px;
            overflow: hidden;
        }
        .method-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .pros-cons {
            border-radius: 8px;
            padding: 16px;
        }
        .pros {
            border-left: 4px solid #4CAF50;
        }
        .cons {
            border-left: 4px solid #F44336;
        }
        .mermaid {
            background-color: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        .drop-cap:first-letter {
            float: left;
            font-size: 3.5em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            color: #667eea;
            font-weight: 700;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-4 font-serif">Java线程创建与管理全指南</h1>
            <p class="text-xl md:text-2xl mb-8 font-light opacity-90">探索Java多线程编程的多种实现方式与最佳实践</p>
            <div class="flex justify-center space-x-4">
                <a href="#methods" class="bg-white text-indigo-600 px-6 py-3 rounded-full font-medium hover:bg-gray-100 transition duration-300">
                    <i class="fas fa-code mr-2"></i>查看实现方法
                </a>
                <a href="#comparison" class="border-2 border-white px-6 py-3 rounded-full font-medium hover:bg-white hover:bg-opacity-10 transition duration-300">
                    <i class="fas fa-chart-bar mr-2"></i>方法对比
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <p class="text-lg text-gray-700 drop-cap">在Java中，有多种方式可以创建和管理线程。每种方式适用于不同的场景，了解这些方式的特点和使用方法可以帮助你在编程时做出合适的选择。</p>
        </section>

        <!-- Methods Section -->
        <section id="methods" class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-2 font-serif">Java线程创建方法</h2>

            <!-- Method 1 -->
            <div class="method-card bg-white shadow-md mb-12">
                <div class="p-6 sm:p-8">
                    <div class="flex items-center mb-4">
                        <span class="text-2xl font-bold text-indigo-600 mr-3">01</span>
                        <h3 class="text-2xl font-bold text-gray-800">继承 <code class="px-2 py-1 bg-gray-100 rounded">Thread</code> 类</h3>
                    </div>
                    <p class="text-gray-700 mb-6">通过继承 <code>Thread</code> 类并重写其 <code>run()</code> 方法来创建线程。这是最基本的线程创建方式。</p>
                    
                    <div class="grid md:grid-cols-2 gap-6 mb-6">
                        <div class="pros-cons pros bg-green-50">
                            <h4 class="font-bold text-green-700 mb-3 flex items-center">
                                <i class="fas fa-check-circle mr-2"></i>优点
                            </h4>
                            <ul class="space-y-2 text-green-800">
                                <li class="flex items-start">
                                    <i class="fas fa-check mt-1 mr-2 text-sm"></i>
                                    <span>简单易用</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check mt-1 mr-2 text-sm"></i>
                                    <span>直接继承 <code>Thread</code> 类，便于访问线程方法</span>
                                </li>
                            </ul>
                        </div>
                        <div class="pros-cons cons bg-red-50">
                            <h4 class="font-bold text-red-700 mb-3 flex items-center">
                                <i class="fas fa-times-circle mr-2"></i>缺点
                            </h4>
                            <ul class="space-y-2 text-red-800">
                                <li class="flex items-start">
                                    <i class="fas fa-times mt-1 mr-2 text-sm"></i>
                                    <span>不能继承其他类，因为 Java 不支持多重继承</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-times mt-1 mr-2 text-sm"></i>
                                    <span>在实际开发中，这种方法相对较少用，因为它不符合 Java 的面向对象设计原则</span>
                                </li>
                            </ul>
                        </div>
                    </div>

                    <div class="code-block">
                        <div class="code-header">
                            <span class="text-gray-400 text-sm">Java</span>
                            <button class="code-copy-btn">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <pre class="text-gray-200"><code>class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("Thread is running.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start(); // 启动线程
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <!-- Method 2 -->
            <div class="method-card bg-white shadow-md mb-12">
                <div class="p-6 sm:p-8">
                    <div class="flex items-center mb-4">
                        <span class="text-2xl font-bold text-indigo-600 mr-3">02</span>
                        <h3 class="text-2xl font-bold text-gray-800">实现 <code class="px-2 py-1 bg-gray-100 rounded">Runnable</code> 接口</h3>
                    </div>
                    <p class="text-gray-700 mb-6">通过实现 <code>Runnable</code> 接口并实现其 <code>run()</code> 方法来创建线程。这是推荐的创建线程的方式，因为它遵循了 Java 的设计原则，可以让类继承自其他类。</p>
                    
                    <div class="grid md:grid-cols-2 gap-6 mb-6">
                        <div class="pros-cons pros bg-green-50">
                            <h4 class="font-bold text-green-700 mb-3 flex items-center">
                                <i class="fas fa-check-circle mr-2"></i>优点
                            </h4>
                            <ul class="space-y-2 text-green-800">
                                <li class="flex items-start">
                                    <i class="fas fa-check mt-1 mr-2 text-sm"></i>
                                    <span>可以让类继承其他类，因为 <code>Runnable</code> 接口不强制类继承</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check mt-1 mr-2 text-sm"></i>
                                    <span>更符合面向对象编程原则</span>
                                </li>
                            </ul>
                        </div>
                        <div class="pros-cons cons bg-red-50">
                            <h4 class="font-bold text-red-700 mb-3 flex items-center">
                                <i class="fas fa-times-circle mr-2"></i>缺点
                            </h4>
                            <ul class="space-y-2 text-red-800">
                                <li class="flex items-start">
                                    <i class="fas fa-times mt-1 mr-2 text-sm"></i>
                                    <span>需要通过 <code>Thread</code> 对象来启动线程</span>
                                </li>
                            </ul>
                        </div>
                    </div>

                    <div class="code-block">
                        <div class="code-header">
                            <span class="text-gray-400 text-sm">Java</span>
                            <button class="code-copy-btn">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <pre class="text-gray-200"><code>class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("Thread is running.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        thread.start(); // 启动线程
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <!-- Method 3 -->
            <div class="method-card bg-white shadow-md mb-12">
                <div class="p-6 sm:p-8">
                    <div class="flex items-center mb-4">
                        <span class="text-2xl font-bold text-indigo-600 mr-3">03</span>
                        <h3 class="text-2xl font-bold text-gray-800">使用 <code class="px-2 py-1 bg-gray-100 rounded">Callable</code> 接口和 <code class="px-2 py-1 bg-gray-100 rounded">Future</code></h3>
                    </div>
                    <p class="text-gray-700 mb-6">通过实现 <code>Callable</code> 接口，并使用 <code>Future</code> 来获取线程的执行结果。与 <code>Runnable</code> 接口不同，<code>Callable</code> 可以返回结果，并且可以抛出异常。</p>
                    
                    <div class="grid md:grid-cols-2 gap-6 mb-6">
                        <div class="pros-cons pros bg-green-50">
                            <h4 class="font-bold text-green-700 mb-3 flex items-center">
                                <i class="fas fa-check-circle mr-2"></i>优点
                            </h4>
                            <ul class="space-y-2 text-green-800">
                                <li class="flex items-start">
                                    <i class="fas fa-check mt-1 mr-2 text-sm"></i>
                                    <span>可以返回结果并处理异常</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check mt-1 mr-2 text-sm"></i>
                                    <span>提供了更加灵活的线程管理和结果获取机制</span>
                                </li>
                            </ul>
                        </div>
                        <div class="pros-cons cons bg-red-50">
                            <h4 class="font-bold text-red-700 mb-3 flex items-center">
                                <i class="fas fa-times-circle mr-2"></i>缺点
                            </h4>
                            <ul class="space-y-2 text-red-800">
                                <li class="flex items-start">
                                    <i class="fas fa-times mt-1 mr-2 text-sm"></i>
                                    <span>需要使用 <code>ExecutorService</code> 来管理线程，并且增加了编程复杂度</span>
                                </li>
                            </ul>
                        </div>
                    </div>

                    <div class="code-block">
                        <div class="code-header">
                            <span class="text-gray-400 text-sm">Java</span>
                            <button class="code-copy-btn">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <pre class="text-gray-200"><code>import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

class MyCallable implements Callable&lt;String&gt; {
    @Override
    public String call() throws Exception {
        return "Callable result";
    }
}

public class Main {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(1);
        MyCallable myCallable = new MyCallable();
        Future&lt;String&gt; future = executor.submit(myCallable);

        try {
            String result = future.get(); // 获取线程执行结果
            System.out.println(result);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

        executor.shutdown();
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <!-- Method 4 -->
            <div class="method-card bg-white shadow-md mb-12">
                <div class="p-6 sm:p-8">
                    <div class="flex items-center mb-4">
                        <span class="text-2xl font-bold text-indigo-600 mr-3">04</span>
                        <h3 class="text-2xl font-bold text-gray-800">使用 <code class="px-2 py-1 bg-gray-100 rounded">ExecutorService</code> 和线程池</h3>
                    </div>
                    <p class="text-gray-700 mb-6">通过 <code>ExecutorService</code> 创建和管理线程池，提供了对线程生命周期的管理，减少了创建和销毁线程的开销。</p>
                    
                    <div class="grid md:grid-cols-2 gap-6 mb-6">
                        <div class="pros-cons pros bg-green-50">
                            <h4 class="font-bold text-green-700 mb-3 flex items-center">
                                <i class="fas fa-check-circle mr-2"></i>优点
                            </h4>
                            <ul class="space-y-2 text-green-800">
                                <li class="flex items-start">
                                    <i class="fas fa-check mt-1 mr-2 text-sm"></i>
                                    <span>提供了线程池的管理，减少了线程创建和销毁的开销</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check mt-1 mr-2 text-sm"></i>
                                    <span>可以管理线程的生命周期、执行任务的调度等</span>
                                </li>
                            </ul>
                        </div>
                        <div class="pros-cons cons bg-red-50">
                            <h4 class="font-bold text-red-700 mb-3 flex items-center">
                                <i class="fas fa-times-circle mr-2"></i>缺点
                            </h4>
                            <ul class="space-y-2 text-red-800">
                                <li class="flex items-start">
                                    <i class="fas fa-times mt-1 mr-2 text-sm"></i>
                                    <span>配置和使用较复杂</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-times mt-1 mr-2 text-sm"></i>
                                    <span>需要额外的管理和监控</span>
                                </li>
                            </ul>
                        </div>
                    </div>

                    <div class="code-block">
                        <div class="code-header">
                            <span class="text-gray-400 text-sm">Java</span>
                            <button class="code-copy-btn">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <pre class="text-gray-200"><code>import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(3); // 创建一个固定大小的线程池

        for (int i = 0; i < 5; i++) {
            executor.execute(() -> {
                System.out.println("Thread is running: " + Thread.currentThread().getName());
            });
        }

        executor.shutdown(); // 关闭线程池
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <!-- Method 5 -->
            <div class="method-card bg-white shadow-md">
                <div class="p-6 sm:p-8">
                    <div class="flex items-center mb-4">
                        <span class="text-2xl font-bold text-indigo-600 mr-3">05</span>
                        <h3 class="text-2xl font-bold text-gray-800">使用 <code class="px-2 py-1 bg-gray-100 rounded">ForkJoinPool</code></h3>
                    </div>
                    <p class="text-gray-700 mb-6"><code>ForkJoinPool</code> 用于并行处理任务，支持任务的分割和合并，适合处理大规模的任务。</p>
                    
                    <div class="grid md:grid-cols-2 gap-6 mb-6">
                        <div class="pros-cons pros bg-green-50">
                            <h4 class="font-bold text-green-700 mb-3 flex items-center">
                                <i class="fas fa-check-circle mr-2"></i>优点
                            </h4>
                            <ul class="space-y-2 text-green-800">
                                <li class="flex items-start">
                                    <i class="fas fa-check mt-1 mr-2 text-sm"></i>
                                    <span>高效处理大规模的任务</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-check mt-1 mr-2 text-sm"></i>
                                    <span>支持任务的拆分和合并，优化任务的执行</span>
                                </li>
                            </ul>
                        </div>
                        <div class="pros-cons cons bg-red-50">
                            <h4 class="font-bold text-red-700 mb-3 flex items-center">
                                <i class="fas fa-times-circle mr-2"></i>缺点
                            </h4>
                            <ul class="space-y-2 text-red-800">
                                <li class="flex items-start">
                                    <i class="fas fa-times mt-1 mr-2 text-sm"></i>
                                    <span>配置和使用较复杂</span>
                                </li>
                                <li class="flex items-start">
                                    <i class="fas fa-times mt-1 mr-2 text-sm"></i>
                                    <span>主要适用于计算密集型任务</span>
                                </li>
                            </ul>
                        </div>
                    </div>

                    <div class="code-block">
                        <div class="code-header">
                            <span class="text-gray-400 text-sm">Java</span>
                            <button class="code-copy-btn">
                                <i class="far fa-copy"></i>
                            </button>
                        </div>
                        <pre class="text-gray-200"><code>import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

class MyTask extends RecursiveTask&lt;Integer&gt; {
    private final int workload;

    public MyTask(int workload) {
        this.workload = workload;
    }

    @Override
    protected Integer compute() {
        if (workload > 10) {
            MyTask task1 = new MyTask(workload / 2);
            MyTask task2 = new MyTask(workload / 2);
            task1.fork(); // 异步执行
            task2.fork();
            return task1.join() + task2.join(); // 合并结果
        } else {
            return workload;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        ForkJoinPool pool = new ForkJoinPool();
        MyTask task = new MyTask(100);
        Integer result = pool.invoke(task);
        System.out.println("Result: " + result);
    }
}</code></pre>
                    </div>
                </div>
            </div>
        </section>

        <!-- Comparison Section -->
        <section id="comparison" class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-2 font-serif">方法对比</h2>
            
            <div class="bg-white shadow-md rounded-lg overflow-hidden">
                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">方法</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">适用场景</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">复杂度</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">返回值</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">异常处理</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">继承Thread类</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">简单任务、学习使用</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">低</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">无</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">有限</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">实现Runnable接口</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">通用场景、推荐方式</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">低</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">无</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">有限</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">Callable + Future</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">需要返回结果的任务</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">中</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">支持</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">支持</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">ExecutorService</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">线程池管理、并发任务</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">中高</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">可选</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">支持</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap font-medium text-gray-900">ForkJoinPool</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">大规模并行计算任务</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">高</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">支持</td>
                                <td class="px-6 py-4 whitespace-nowrap text-gray-500">支持</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-2 font-serif">线程方法关系图</h2>
            <div class="mermaid">
                graph TD
                    A[Thread] -->|继承| B[Thread类]
                    B -->|实现| C[线程任务]
                    A -->|实现| D[Runnable接口]
                    D --> E[无返回值]
                    A -->|实现| F[Callable接口]
                    F --> G[有返回值]
                    A --> H[Executor框架]
                    H --> I[ExecutorService]
                    I --> J[线程池]
                    H --> K[ForkJoinPool]
                    K --> L[分治任务]
            </div>
        </section>

        <!-- Additional Resources -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 border-b pb-2 font-serif">扩展阅读</h2>
            <div class="bg-white shadow-md rounded-lg overflow-hidden">
                <div class="p-6">
                    <h3 class="text-xl font-bold mb-4 text-indigo-600">线程基本知识</h3>
                    <p class="text-gray-700 mb-4">了解更多关于Java线程的基础知识，包括线程生命周期、同步机制、线程安全等内容。</p>
                    <a href="https://www.yuque.com/jtostring/qiwsg9/katfsm?singleDoc# 《3、线程基本知识》" class="inline-flex items-center text-indigo-600 hover:text-indigo-800 transition duration-300">
                        <span>阅读更多</span>
                        <i class="fas fa-arrow-right ml-2"></i>
                    </a>
                </div>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-white py-8">
        <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-xl font-bold">技术小馆</h3>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-300 hover:text-white transition duration-300">
                        <i class="fas fa-link mr-2"></i>http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });

        // Copy code functionality
        document.querySelectorAll('.code-copy-btn').forEach(button => {
            button.addEventListener('click', () => {
                const codeBlock = button.closest('.code-block').querySelector('code');
                const range = document.createRange();
                range.selectNode(codeBlock);
                window.getSelection().removeAllRanges();
                window.getSelection().addRange(range);
                document.execCommand('copy');
                window.getSelection().removeAllRanges();
                
                // Show copied feedback
                const originalIcon = button.innerHTML;
                button.innerHTML = '<i class="fas fa-check"></i>';
                setTimeout(() => {
                    button.innerHTML = originalIcon;
                }, 2000);
            });
        });
    </script>
</body>
</html>